package robocup.component.coach;

import robocup.component.ControllerNumbers.PLAY_MODE;
import robocup.component.infotypes.AuralInfo;
import robocup.component.infotypes.CoachVisualInfo;
import robocup.component.infotypes.PlayModeInfo;
import robocup.component.infotypes.PlayerSayInfo;
import robocup.component.infotypes.PlayerTypesInfo;
import robocup.component.infotypes.RefereeInfo;
import robocup.component.infotypes.SenseBodyInfo;
import robocup.component.infotypes.VisualInfo;
import robocup.component.speechacts.MessageFactory;
import robocup.component.worldobjects.Player;

/**
 * PositionAnalyzer sends the positions of all objects to the players this is
 * done with freeform messages and only beforekickoff
 * 
 * because of name it maybe should analyze the positions 
 * too.. :)
 * 
 * @author phil
 */
public class PositionAnalyzer
    implements CoachInfoReceiver {

  // CoachWorld
  private CoachWorld    cwm;

  // OLCoachTactic
  private OLCoachTactic olct;

  private String        myMemory = "";

  public PositionAnalyzer(
      final OLCoachTactic ol) {

    this.olct = ol;
    this.cwm = ol.getCwm();
  }

  /**
   * Called by Perception at the arrival of an AuralInfo.
   * 
   * @param info
   */
  public void update(final AuralInfo info) {

  }

  /**
   * Called by Perception at the arrival of a VisualInfo.
   * 
   * @param info
   */
  public void update(final CoachVisualInfo info) {

    this.sendPositions();
  }

  /**
   * Called by Perception at the arrival of Player Types.
   * 
   * @param info
   */
  public void update(final PlayerTypesInfo info) {

  }

  /**
   * This method sends by MessageFactory encoded Positions of Players, Opponents
   * and a Ball.
   * 
   * 
   */
  public void sendPositions() {

    if (this.cwm.getPlayMode() == PLAY_MODE.BEFORE_KICK_OFF) {
      final MessageFactory mf = this.cwm.getMessageFactory();

      final StringBuffer buf = new StringBuffer();

      // Ballposition
      buf.append(mf.encodeBallPosition(this.cwm.getBall(), 0));

      // Playerpositions
      for (int i = 0; i < 11; i++) {
        if (this.cwm.getTeammates()[i].isGoalie()) {
        	Player player = this.cwm.getTeammates()[i].cloned();
        	if (this.cwm.isTeamEast())
        		player.getPosition().rotate(180);
        	buf.append(mf.encodeGoaliePosition(player, 0));
        }
        else {
        	Player player = this.cwm.getTeammates()[i].cloned();
        	if (this.cwm.isTeamEast())
        		player.getPosition().rotate(180);
        	buf.append(mf.encodePlayerPosition(player, 0));
          // buf.append(mf.encodePlayerPosition(this.cwm.getTeammates()[i], 0));
        }
      }

      // Opponentpositions
      for (int i = 0; i < 11; i++) {
        if (this.cwm.getOpponents()[i].isGoalie()) {
        	Player player = this.cwm.getOpponents()[i].cloned();
        	if (this.cwm.isTeamEast())
        		player.getPosition().rotate(180);
        	buf.append(mf.encodeGoaliePosition(player, 0));
          // buf.append(mf.encodeGoaliePosition(this.cwm.getOpponents()[i], 0));
        }
        else {
        	Player player = this.cwm.getOpponents()[i].cloned();
        	if (this.cwm.isTeamEast())
        		player.getPosition().rotate(180);
        	buf.append(mf.encodePlayerPosition(player, 0));
          // buf.append(mf.encodePlayerPosition(this.cwm.getOpponents()[i], 0));
        }
      }

      // Memorycheck
      if (!this.myMemory.equals(buf.toString())) {
        // System.out.println(buf.toString());
        this.olct.sendFreeform(buf.toString());
        this.myMemory = buf.toString();
      }
    }
  }

  /* (non-Javadoc)
   * @see robocup.component.infotypes.InfoReceiver#update(robocup.component.infotypes.SenseBodyInfo)
   */
  public void update(final SenseBodyInfo info) {

    // TODO Auto-generated method stub
    
  }

  /* (non-Javadoc)
   * @see robocup.component.infotypes.InfoReceiver#update(robocup.component.infotypes.VisualInfo)
   */
  public void update(final VisualInfo info) {

    // TODO Auto-generated method stub
    
  }

  /* (non-Javadoc)
   * @see robocup.component.infotypes.InfoReceiver#update(robocup.component.infotypes.RefereeInfo)
   */
  public void update(final RefereeInfo info) {

    // TODO Auto-generated method stub
    
  }

  /* (non-Javadoc)
   * @see robocup.component.infotypes.InfoReceiver#update(robocup.component.infotypes.PlayModeInfo)
   */
  public void update(final PlayModeInfo info) {

    // TODO Auto-generated method stub
    
  }

  /* (non-Javadoc)
   * @see robocup.component.infotypes.InfoReceiver#update(robocup.component.infotypes.PlayerSayInfo)
   */
  public void update(final PlayerSayInfo info) {

    // TODO Auto-generated method stub
    
  }
}
